/**HEADER*********************************************************************
*
* Copyright (c) 2008 Freescale Semiconductor;
* All Rights Reserved
*
******************************************************************************
*
* THIS SOFTWARE IS PROVIDED BY FREESCALE "AS IS" AND ANY EXPRESSED OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL FREESCALE OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*
* $FileName: dac_pdd.h$
* $Version : 3.8.2.1$
* $Date    : Mar-1-2012$
*
* Comments:
*
*
*
*END*************************************************************************/

#ifndef __dac_pdd_h__
    #define __dac_pdd_h__


/* ============================================================================
   ================== General PDD macros ======================================
   ============================================================================ */

#define PDD_DISABLE                         0u
#define PDD_ENABLE                          1u

/* PDD constants definitions */
#define DAC_PDD_DISABLE                     0x00u
#define DAC_PDD_ENABLE                      0x80u
#define DAC_PDD_BUFFER_WATERMARK_INT        0x04u
#define DAC_PDD_BUFFER_END_INT              0x02u
#define DAC_PDD_BUFFER_START_INT            0x01u
#define DAC_PDD_HW_TRIGGER                  0x00u
#define DAC_PDD_SW_TRIGGER                  0x20u

/* D/A converter's buffer work mode constant. */
#define DAC_PDD_BUFFER_NORMAL_MODE          0x00u
#define DAC_PDD_BUFFER_SWING_MODE           0x02u
#define DAC_PDD_BUFFER_OTSCAN_MODE          0x04u

/* D/A converter's buffer watermark constant. For exact value representation see
   peripheral device documentation. */
#define DAC_PDD_BUFFER_WATERMARK_L1         0x00u
#define DAC_PDD_BUFFER_WATERMARK_L2         0x08u
#define DAC_PDD_BUFFER_WATERMARK_L3         0x10u
#define DAC_PDD_BUFFER_WATERMARK_L4         0x18u

#define DAC_PDD_BUFFER_DISABLE              0x00u
#define DAC_PDD_BUFFER_ENABLE               0x01u

#define DAC_PDD_V_REF_INT                   0x00u
#define DAC_PDD_V_REF_EXT                   0x40u

/* D/A converter's buffer low power mode constant. */
#define DAC_PDD_HIGH_POWER                  0x00u
#define DAC_PDD_LOW_POWER                   0x08u


/* ----------------------------------------------------------------------------
   -- EnableDevice
   ---------------------------------------------------------------------------- */

/**
 * Enables the D/A converter's device.
 * @param peripheralBase Peripheral base address.
 * @param State Requested state of D/A converter.
 */
#define DAC_PDD_EnableDevice(peripheralBase, State) ( \
    DAC_C0_REG(peripheralBase) = \
     (uint8_t)(( \
      (uint8_t)(DAC_C0_REG(peripheralBase) & (uint8_t)(~(uint8_t)DAC_C0_DACEN_MASK))) | ( \
      (uint8_t)((uint8_t)(State) << DAC_C0_DACEN_SHIFT))) \
  )

/* ----------------------------------------------------------------------------
   -- GetDeviceEnabled
   ---------------------------------------------------------------------------- */

/**
 * Returns current state of D/A converter enable bit.
 * @param peripheralBase Peripheral base address.
 */
#define DAC_PDD_GetDeviceEnabled(peripheralBase) ( \
    (uint8_t)(DAC_C0_REG(peripheralBase) & DAC_C0_DACEN_MASK) \
  )

/* ----------------------------------------------------------------------------
   -- EnableInterrupts
   ---------------------------------------------------------------------------- */

/**
 * Enables interrupts defined by mask parameter.
 * @param peripheralBase Peripheral base address.
 * @param Mask Mask of interrupts to enable. Use constants from group
 *        "Interrupts' masks.".
 */
#define DAC_PDD_EnableInterrupts(peripheralBase, Mask) ( \
    DAC_C0_REG(peripheralBase) |= \
     (uint8_t)(Mask) \
  )

/* ----------------------------------------------------------------------------
   -- DisableInterrupts
   ---------------------------------------------------------------------------- */

/**
 * Disables interrupts defined by mask parameter.
 * @param peripheralBase Peripheral base address.
 * @param Mask Mask of interrupts to disable. Use constants from group
 *        "Interrupts' masks.".
 */
#define DAC_PDD_DisableInterrupts(peripheralBase, Mask) ( \
    DAC_C0_REG(peripheralBase) &= \
     (uint8_t)(~(uint8_t)(Mask)) \
  )

/* ----------------------------------------------------------------------------
   -- SetInterruptMask
   ---------------------------------------------------------------------------- */

/**
 * Sets all interrupts with value according to mask parameter.
 * @param peripheralBase Peripheral base address.
 * @param Mask Mask of interrupts to enable - rest will be disabled. Use
 *        constants from group "Interrupts' masks.".
 */
#define DAC_PDD_SetInterruptMask(peripheralBase, Mask) ( \
    DAC_C0_REG(peripheralBase) = \
     (uint8_t)(( \
      (uint8_t)(( \
       DAC_C0_REG(peripheralBase)) & ( \
       (uint8_t)(~(uint8_t)(DAC_C0_DACBBIEN_MASK | (DAC_C0_DACBTIEN_MASK | DAC_C0_DACBWIEN_MASK)))))) | ( \
      (uint8_t)(Mask))) \
  )

/* ----------------------------------------------------------------------------
   -- GetInterruptMask
   ---------------------------------------------------------------------------- */

/**
 * Returns interrupts mask.
 * @param peripheralBase Peripheral base address.
 * @return Use constants from group "Interrupts' masks." for processing return
 *         value.
 */
#define DAC_PDD_GetInterruptMask(peripheralBase) ( \
    (uint8_t)(( \
     DAC_C0_REG(peripheralBase)) & ( \
     (uint8_t)(DAC_C0_DACBBIEN_MASK | (DAC_C0_DACBTIEN_MASK | DAC_C0_DACBWIEN_MASK)))) \
  )

/* ----------------------------------------------------------------------------
   -- GetInterruptFlags
   ---------------------------------------------------------------------------- */

/**
 * Returns interrupt flag bits.
 * @param peripheralBase Peripheral base address.
 * @return Use constants from group "Interrupts' flags." for processing return
 *         value.
 */
#define DAC_PDD_GetInterruptFlags(peripheralBase) ( \
    (uint8_t)(( \
     DAC_SR_REG(peripheralBase)) & ( \
     (uint8_t)(DAC_SR_DACBFRPBF_MASK | (DAC_SR_DACBFRPTF_MASK | DAC_SR_DACBFWMF_MASK)))) \
  )

/* ----------------------------------------------------------------------------
   -- ClearInterruptFlags
   ---------------------------------------------------------------------------- */

/**
 * Clears interrupt flag bits defined by mask parameter.
 * @param peripheralBase Peripheral base address.
 * @param Mask Mask of interrupt's flags to clear. Use constants from group
 *        "Interrupts' flags.".
 */
#define DAC_PDD_ClearInterruptFlags(peripheralBase, Mask) ( \
    DAC_SR_REG(peripheralBase) &= \
     (uint8_t)(~(uint8_t)(Mask)) \
  )

/* ----------------------------------------------------------------------------
   -- SetData
   ---------------------------------------------------------------------------- */

/**
 * Sets value of D/A converter's data buffer word defined by Index parameter.
 * @param peripheralBase Peripheral base address.
 * @param Data Data word value.
 * @param RegIndex Buffer word index.
 */
#define DAC_PDD_SetData(peripheralBase, Data, RegIndex) ( \
    (DAC_DATL_REG(peripheralBase,(RegIndex)) = \
     (uint8_t)(Data)), \
    (DAC_DATH_REG(peripheralBase,(RegIndex)) = \
     (uint8_t)(( \
      (uint8_t)(( \
       DAC_DATH_REG(peripheralBase,(RegIndex))) & ( \
       (uint8_t)(~(uint8_t)DAC_DATH_DATA_MASK)))) | ( \
      (uint8_t)((uint16_t)(Data) >> 8U)))) \
  )

/* ----------------------------------------------------------------------------
   -- EnableBuffer
   ---------------------------------------------------------------------------- */

/**
 * D/A converter's buffer enable.
 * @param peripheralBase Peripheral base address.
 * @param State Parameter specifying if D/A converter's buffer will be enabled
 *        or disabled.
 */
#define DAC_PDD_EnableBuffer(peripheralBase, State) ( \
    DAC_C1_REG(peripheralBase) = \
     (uint8_t)(( \
      (uint8_t)(DAC_C1_REG(peripheralBase) & (uint8_t)(~(uint8_t)DAC_C1_DACBFEN_MASK))) | ( \
      (uint8_t)(State))) \
  )

/* ----------------------------------------------------------------------------
   -- GetBufferEnabled
   ---------------------------------------------------------------------------- */

/**
 * Returns current data buffer state.
 * @param peripheralBase Peripheral base address.
 */
#define DAC_PDD_GetBufferEnabled(peripheralBase) ( \
    (uint8_t)(DAC_C1_REG(peripheralBase) & DAC_C1_DACBFEN_MASK) \
  )

/* ----------------------------------------------------------------------------
   -- SetTrigger
   ---------------------------------------------------------------------------- */

/**
 * Selects D/A converter's trigger.
 * @param peripheralBase Peripheral base address.
 * @param Trigger Parameter specifying which trigger source will be used.
 */
#define DAC_PDD_SetTrigger(peripheralBase, Trigger) ( \
    DAC_C0_REG(peripheralBase) = \
     (uint8_t)(( \
      (uint8_t)(DAC_C0_REG(peripheralBase) & (uint8_t)(~(uint8_t)DAC_C0_DACTRGSEL_MASK))) | ( \
      (uint8_t)(Trigger))) \
  )

/* ----------------------------------------------------------------------------
   -- GetTriggerSource
   ---------------------------------------------------------------------------- */

/**
 * Returns current trigger source.
 * @param peripheralBase Peripheral base address.
 */
#define DAC_PDD_GetTriggerSource(peripheralBase) ( \
    (uint8_t)(DAC_C0_REG(peripheralBase) & DAC_C0_DACTRGSEL_MASK) \
  )

/* ----------------------------------------------------------------------------
   -- ForceSwTrigger
   ---------------------------------------------------------------------------- */

/**
 * Forces D/A converter's software trigger.
 * @param peripheralBase Peripheral base address.
 */
#define DAC_PDD_ForceSwTrigger(peripheralBase) ( \
    DAC_C0_REG(peripheralBase) |= \
     DAC_C0_DACSWTRG_MASK \
  )

/* ----------------------------------------------------------------------------
   -- SetBufferMode
   ---------------------------------------------------------------------------- */

/**
 * Selects D/A converter's buffer work mode.
 * @param peripheralBase Peripheral base address.
 * @param Mode Parameter specifying which data buffer work mode will be used.
 */
#define DAC_PDD_SetBufferMode(peripheralBase, Mode) ( \
    DAC_C1_REG(peripheralBase) = \
     (uint8_t)(( \
      (uint8_t)(DAC_C1_REG(peripheralBase) & (uint8_t)(~(uint8_t)DAC_C1_DACBFMD_MASK))) | ( \
      (uint8_t)((uint8_t)(Mode) << DAC_C1_DACBFMD_SHIFT))) \
  )

/* ----------------------------------------------------------------------------
   -- SetBufferWatermark
   ---------------------------------------------------------------------------- */

/**
 * Sets D/A converter's buffer watermark.
 * @param peripheralBase Peripheral base address.
 * @param Watermark Parameter specifying data buffer watermark level.
 */
#define DAC_PDD_SetBufferWatermark(peripheralBase, Watermark) ( \
    DAC_C1_REG(peripheralBase) = \
     (uint8_t)(( \
      (uint8_t)(DAC_C1_REG(peripheralBase) & (uint8_t)(~(uint8_t)DAC_C1_DACBFWM_MASK))) | ( \
      (uint8_t)((uint8_t)(Watermark) << DAC_C1_DACBFWM_SHIFT))) \
  )

/* ----------------------------------------------------------------------------
   -- SetBufferReadPointer
   ---------------------------------------------------------------------------- */

/**
 * Sets D/A converter's buffer read pointer.
 * @param peripheralBase Peripheral base address.
 * @param Pointer Parameter specifying new data buffer read pointer value.
 */
#define DAC_PDD_SetBufferReadPointer(peripheralBase, Pointer) ( \
    DAC_C2_REG(peripheralBase) = \
     (uint8_t)(( \
      (uint8_t)(DAC_C2_REG(peripheralBase) & (uint8_t)(~(uint8_t)DAC_C2_DACBFRP_MASK))) | ( \
      (uint8_t)((uint8_t)(Pointer) << DAC_C2_DACBFRP_SHIFT))) \
  )

/* ----------------------------------------------------------------------------
   -- GetBufferReadPointer
   ---------------------------------------------------------------------------- */

/**
 * Returns current data buffer read pointer value.
 * @param peripheralBase Peripheral base address.
 */
#define DAC_PDD_GetBufferReadPointer(peripheralBase) ( \
    (uint8_t)(( \
     (uint8_t)(DAC_C2_REG(peripheralBase) & DAC_C2_DACBFRP_MASK)) >> ( \
     DAC_C2_DACBFRP_SHIFT)) \
  )

/* ----------------------------------------------------------------------------
   -- SetBufferSize
   ---------------------------------------------------------------------------- */

/**
 * Sets D/A converter's buffer upper limit.
 * @param peripheralBase Peripheral base address.
 * @param Size Buffer upper limit (buffer register maximal index).
 */
#define DAC_PDD_SetBufferSize(peripheralBase, Size) ( \
    DAC_C2_REG(peripheralBase) = \
     (uint8_t)(( \
      (uint8_t)(DAC_C2_REG(peripheralBase) & (uint8_t)(~(uint8_t)DAC_C2_DACBFUP_MASK))) | ( \
      (uint8_t)(Size))) \
  )

/* ----------------------------------------------------------------------------
   -- GetBufferSize
   ---------------------------------------------------------------------------- */

/**
 * Returns current data buffer size.
 * @param peripheralBase Peripheral base address.
 */
#define DAC_PDD_GetBufferSize(peripheralBase) ( \
    (uint8_t)(DAC_C2_REG(peripheralBase) & DAC_C2_DACBFUP_MASK) \
  )

/* ----------------------------------------------------------------------------
   -- SetReference
   ---------------------------------------------------------------------------- */

/**
 * Selects D/A converter's reference.
 * @param peripheralBase Peripheral base address.
 * @param Reference Parameter specifying if internal or external voltage
 *        reference source will be used.
 */
#define DAC_PDD_SetReference(peripheralBase, Reference) ( \
    DAC_C0_REG(peripheralBase) = \
     (uint8_t)(( \
      (uint8_t)(DAC_C0_REG(peripheralBase) & (uint8_t)(~(uint8_t)DAC_C0_DACRFS_MASK))) | ( \
      (uint8_t)(Reference))) \
  )

/* ----------------------------------------------------------------------------
   -- SetPowerMode
   ---------------------------------------------------------------------------- */

/**
 * Selects D/A converter's low power mode.
 * @param peripheralBase Peripheral base address.
 * @param Mode Parameter specifying if high or low power mode will be used.
 */
#define DAC_PDD_SetPowerMode(peripheralBase, Mode) ( \
    DAC_C0_REG(peripheralBase) = \
     (uint8_t)(( \
      (uint8_t)(DAC_C0_REG(peripheralBase) & (uint8_t)(~(uint8_t)DAC_C0_LPEN_MASK))) | ( \
      (uint8_t)(Mode))) \
  )

/* ----------------------------------------------------------------------------
   -- EnableDmaRequest
   ---------------------------------------------------------------------------- */

/**
 * DMA enable control.
 * @param peripheralBase Peripheral base address.
 * @param State Parameter specifying if DMA requests will be enabled or disabled.
 */
#define DAC_PDD_EnableDmaRequest(peripheralBase, State) ( \
    DAC_C1_REG(peripheralBase) = \
     (uint8_t)(( \
      (uint8_t)(DAC_C1_REG(peripheralBase) & (uint8_t)(~(uint8_t)DAC_C1_DMAEN_MASK))) | ( \
      (uint8_t)((uint8_t)(State) << DAC_C1_DMAEN_SHIFT))) \
  )


#ifdef __cplusplus
extern "C" {
#endif

/*FUNCTION*********************************************************************
*
* Function Name   : _bsp_get_dac_base_address()
*
* Input Params    : dac_module_number - the number of DAC module
*
* Returned Value  : The DAC module base address
*
* Comments        :
*
*END**************************************************************************/


pointer _bsp_get_dac_base_address(uint32_t dac_module_number);

/*FUNCTION*********************************************************************
*
* Function Name   : _bsp_get_dac_vector()
*
* Input Params    : dac_module_number - the number of DAC module
*
* Returned Value  : The DAC module interrupt vector number
*
* Comments        :
*
*END**************************************************************************/

uint32_t _bsp_get_dac_vector(uint32_t dac_module_number);


#ifdef __cplusplus
}
#endif


#endif /* __dac_pdd_h__ */

